// pages/shopping/shopping.js
import { async } from '../../lib/runtime/runtime.js';
import {request} from '../../request/index.js'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    totle:0,
  user_id:null,
  price:null,
  cratList:[],
  checked:[],
  sums:[],
  checkedCatList:[],
  checkedAll:false,
  arrId:'',//地址id
  address:[]
  },
  onChange(e) {
    console.log(e);
    const fid = e.currentTarget.dataset.fid;
     const arr = this.data.checked
     //arr取反
      arr[fid] = !arr[fid]
        if(arr[fid]){
          this.calculatePrice();
        }else{
          this.calculatePrice();
        }
       
      this.setData({
        checked : arr,
       
      })
      // this.setQuanxuan()
      console.log(this.data.checked);
  },
  // 设置全选按钮
  setQuanxuan(){
    var _this = this;
   var  {checked} =this.data;
   var {checkedAll} = this.data;
   checked.forEach((e,i) => {
        if (this.data.cratList.length == checked.length) {
            _this.setData({
              checkedAll:true
            })
        }else{
          _this.setData({
            checkedAll:false
          })
        }
      });
  },
  //计算价格
  calculatePrice(){
      const {checked} = this.data
      const {sums} = this.data
      let price = 0;
      checked.forEach((e,i) => {
            price += sums[i]
    
      });

      checked.forEach((e,i) => {
        if(!e){
          price -=sums[i]
        }
  
  });
       
      
      this.setData({
        price :price
      })
  },

  gotoSum(event){
  console.log();  
    this.setData({
      checkedAll: event.detail
    })
     const {checked} = this.data;
      //把checked设置为全选的属性
    checked.forEach((e,i)=>{
      checked[i]= this.data.checkedAll
      this.setData({
        checked : checked
      })
    })
    //全选时计算价格
    var _this =this;
    if(this.data.checkedAll){
      this.calculatePrice()
   
    }else{
      this.setData({
        price : 0
      })
      
    }

    
  },
  async getShoppingCrat(){
   let {totle} = this.data
    const result =  await request({
      url:  "/shopping_cart/cartList",
      data:{
        user_id :this.data.user_id,
  
      }
    });
    console.log(result.data.list); 
    this.setData({
      cratList :result.data.list
    })
    if(totle==0){
      this.setData({
        totle :1
      })
      this.initChecked()
    }
    
  
    const {sums} = this.data
    this.data.cratList.forEach((e,i) => {
        if(e.number<=1){
         
        }
       sums[i] = e.number * e.amount;
      this.setData({
        sums:sums,
      })
     });
    
     this.calculatePrice()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     var Info=wx.getStorageSync('userInfo');
     //获取用户id
     var Infos=Info.id;

     if (options.arrId!=undefined) {
       this.setData({
        arrId:options.arrId
       })
     }
     this.setData({
      user_id :Infos,
     })

     this.getShoppingCrat();
     this.getAddressList()
  },
  //初始化选择状态
 async initChecked(){
    const {checked} = this.data
   //const {checkedCatList} = this.data
    this.data.cratList.forEach((e,i) => {
      checked[i] = false;
     // checkedCatList[i] = null
      this.setData({
        checked: checked,
      //  checkedCatList:checkedCatList
      })
     })
  },
  // 减少购物车数量
async  bindMinus(e){
  const {spid} = e.currentTarget.dataset
  console.log(e);
    const result =  await request({
      url:  "/shopping_cart/updateCart",
      data:{
        id:spid,
        number:-1
      }
    });
    console.log(result);
    this.getShoppingCrat()
   this.calculatePrice()
  },
  //增加购物车数量
 async bindPlus(e){
    const {spid} = e.currentTarget.dataset
    console.log(e);
      const result =  await request({
        url:  "/shopping_cart/updateCart",
        data:{
          id:spid,
          number:1
        }
      });
      console.log(result);
      this.getShoppingCrat()
   
  },
  //删除购物车数据方法
  jumpdetail(e){
    var _this = this;
    let id=e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定删除吗？',
      success (res) {
        if (res.confirm) {
          _this.getCartDelete(id);
     
          // 刷新页面
          // wx.navigateTo({
          //   url: '/pages/shoucang/index',
          // })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
 async getCartDelete(id){
  const result =  await request({
    url:  "/shopping_cart/deleteCart",
    data:{
      id :id
    }
  });
  this.getShoppingCrat()
 },

async onClickButton(e){
   
  this.getCheckedCrat()
  let sid = this.data.address.id
  let checkedCatList= JSON.stringify(this.data.checkedCatList)
  const result =  await request({
    url:  "orders/add",
    method:"POST",
    data: {sid ,checkedCatList}

  });
  console.log(result);
 
 },
 //获取选择购物车
 getCheckedCrat(){
   const {cratList} = this.data 
    let checkedCatList =[]
    this.data.checked.forEach((e,i)=>{
      if(e){
        checkedCatList.push(cratList[i])
      }
    })
    console.log(checkedCatList);
    this.setData({
      checkedCatList:checkedCatList
    })

 },
async goToAddress(){
    wx.navigateTo({
      url: '/pages/addList/list',
    })
 },
async getAddressList(){
  let uid= this.data.user_id
  let ad =this.data.arrId
  // ad= null
  console.log(ad);
  const result =  await request({
    url:  "/sites/sitesuid",
    data: {
        uid:uid,
        id :ad
    }
  });
  
  console.log(result);
  let address = result.data.list[0]
  this.setData({
    address:address
  })
},
getAddress(){
  wx.navigateTo({
    url: '/pages/addAddress/addAddress',
  })
}
})